﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NLayersExample.InstanceManager;
using NLayersExample.Entity;

namespace NLayersExample.Web
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int count = WebApp.BusinessLogic.SampleBL.GetCustomerCount();

            MessageLabel.Text = "Sample Customer Count: " + count.ToString();
        }

        protected void GetAllButton_Click(object sender, EventArgs e)
        {
            var list = WebApp.DataAccess.SampleCustomerDA.GetAll();

            grid.DataSource = list;
            grid.DataBind();
        }

        protected void InsertButton_Click(object sender, EventArgs e)
        {
            SampleCustomer customer = new SampleCustomer();
            customer.Name = "New Customer";
            customer.Address = "New Address";

            WebApp.DataAccess.SampleCustomerDA.Insert(customer);
        }

        protected void UpdateButton_Click(object sender, EventArgs e)
        {
            SampleCustomer customer = WebApp.DataAccess.SampleCustomerDA.Where(c => c.Name == "New Customer").FirstOrDefault();
            customer.Address += "Updated";
            if (customer != null)
                WebApp.DataAccess.SampleCustomerDA.Update(customer);

            else
                MessageLabel.Text = "Customer Not Found!";
        }

        protected void DeleteButton_Click(object sender, EventArgs e)
        {
            SampleCustomer customer = WebApp.DataAccess.SampleCustomerDA.Where(c => c.Name == "New Customer").FirstOrDefault();

            if (customer != null)
                WebApp.DataAccess.SampleCustomerDA.Delete(customer);

            else
                MessageLabel.Text = "Customer Not Found!";
        }

        protected void DisconnectedButton_Click(object sender, EventArgs e)
        {
            SampleCustomer customer = new SampleCustomer();
            customer.Name = "New Customer";
            customer.Address = "New Address";
            WebApp.DataAccess.SampleCustomerDA.Insert(customer); // Insert in one context

            customer.Name += " Updated";
            WinApp.DataAccess.SampleCustomerDA.Update(customer); // Update from another context

            SampleCustomer newCustomer = new SampleCustomer();
            newCustomer.Name = "Another Customer";
            newCustomer.Address = "Another Address";
            WebApp.DataAccess.SampleCustomerDA.Insert(newCustomer); // Insert in one context

            newCustomer.Name += " Updated";
            WinApp.DataAccess.SampleCustomerDA.Update(newCustomer); // Update from another context

            WebApp.DataAccess.SampleCustomerDA.Delete(newCustomer); // Delete in another context

        }
    }
}